package com.gitee.dupe.dict.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gitee.dolt.annotation.security.PreAuth;
import com.gitee.dolt.boot.controller.SuperController;
import com.gitee.dolt.boot.request.QueryParams;
import com.gitee.dolt.core.base.Result;
import com.gitee.dolt.core.base.entity.EmptyObject;
import com.gitee.dupe.dict.entity.Parameter;
import com.gitee.dupe.dict.service.IParameterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * 参数配置
 * </p>
 *
 * @author LYH
 */
@RestController
@RequestMapping("/b/parameter")
@Validated
@Api(value = "Parameter", tags = "参数配置")
@PreAuth(replace = "b:parameter:")
public class ParameterController extends SuperController<Parameter, IParameterService, Long, Parameter,
        Parameter, Parameter> {

    @Override
    public QueryWrapper<Parameter> handlerQueryWrapper(Parameter parameter, QueryParams<Parameter> params) {
        QueryWrapper<Parameter> qw = Wrappers.query();
        String keyword = params.getKeyword();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.lambda().and(e -> e.like(Parameter::getKey, keyword).or().like(Parameter::getName, keyword));
        }
        Parameter model = params.getModel();
        Boolean readonly = model.getReadonly();
        qw.lambda().eq(readonly != null, Parameter::getReadonly, readonly);
        return qw;
    }

    @GetMapping("/getValueByKeys")
    @ApiOperation(value = "获取值")
    @PreAuth(enabled = false)
    public Result getValueByKeys(@RequestParam("keys[]") List<String> keys) {
        Map<String, String> map = new HashMap<>(keys.size());
        keys.forEach(key -> {
            map.put(key, baseService.getValue(key, null));
        });
        return Result.ok(map);
    }
}
